package com.itheima.mp.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.mp.domain.dto.PageDTO;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import com.itheima.mp.service.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api("用户接口管理")
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;

    @ApiOperation("新增用户")
    @PostMapping
    public void saveUser(@RequestBody UserFormDTO userFormDTO) {
        User user = new User();
        BeanUtils.copyProperties(userFormDTO, user);
        userService.save(user);
    }

    @ApiOperation("删除用户")
    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Long id) {
        userService.removeById(id);
    }

    /**
     * 根据id查询用户
     * @param id 用户id
     * @return 用户信息
     */
    @ApiOperation("根据id查询用户")
    @GetMapping("/{id}")
    public UserVO queryById(@PathVariable Long id) {
//        UserVO userVO = new UserVO();
//        User user = userService.getById(id);
//        BeanUtils.copyProperties(user, userVO);
//        return userVO;
        return userService.queryUserAndAddressById(id);
    }

    /**
     * 根据id批量查询用户
     * @param ids 用户id列表
     * @return 用户列表
     */
    @ApiOperation("根据id批量查询用户")
    @GetMapping
    public List<UserVO> queryByIds(@RequestParam List<Long> ids) {
//        List<User> users = userService.listByIds(ids);
//        return BeanUtil.copyToList(users, UserVO.class);
        return userService.queryUserAndAddressByIds(ids);
    }

    /**
     * 根据id扣减余额
     * @param id 用户id
     * @param amount 扣减金额
     */
    @ApiOperation("根据id扣减余额")
    @PostMapping("/{id}/deduction/{amount}")
    public void updateBalanceById(@PathVariable Long id, @PathVariable Integer amount) {
        userService.deductBalanceById(id, amount);
    }

    /**
     * 根据条件查询用户列表
     * @param userQuery 查询条件
     * @return 用户列表
     */
    @ApiOperation("根据条件查询用户列表")
    @PostMapping("/list")
    public List<UserVO> queryByCondition(@RequestBody UserQuery userQuery) {
        String username = userQuery.getName();
        Integer status = userQuery.getStatus();
        Integer minBalance = userQuery.getMinBalance();
        Integer maxBalance = userQuery.getMaxBalance();
//        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<User>()
//                .like(StrUtil.isNotBlank(username),User::getUsername, username)
//                .eq(status != null,User::getStatus, userQuery.getStatus())
//                .ge(minBalance != null,User::getBalance, userQuery.getMinBalance())
//                .le(maxBalance != null,User::getBalance, userQuery.getMaxBalance());
//        List<User> users = userService.list(wrapper);
        List<User> userList = userService.lambdaQuery()
                .like(StrUtil.isNotBlank(username), User::getUsername, username)
                .eq(status != null, User::getStatus, status)
                .ge(minBalance != null, User::getBalance, minBalance)
                .le(maxBalance != null, User::getBalance, maxBalance)
                .list();
        return BeanUtil.copyToList(userList, UserVO.class);
    }

}
